package com.lv.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.lv.mapper.TbContentCategoryMapper;
import com.lv.mapper.TbContentMapper;
import com.lv.model.TbContent;
import com.lv.model.TbContentCategory;
import com.lv.model.TbContentExample;
import com.lv.service.ContentService;
import com.lv.vo.ContentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {

    @Resource
    private TbContentMapper contentMapper;
    @Resource
    private TbContentCategoryMapper contentCategoryMapper;
    @Resource
    private StringRedisTemplate redis;
    @Override
    public List<ContentVO> getAll() {
        List<TbContent> tbContents = contentMapper.selectByExample(null);
        List<ContentVO> list = new ArrayList();

        for (TbContent tbContent : tbContents) {
            ContentVO vo = new ContentVO();

            //广告ID
            vo.setId(tbContent.getId());

            //标题
            vo.setTitle(tbContent.getTitle());

            //url
            vo.setUrl(tbContent.getUrl());

            //pic
            vo.setPic(tbContent.getPic());

            //排序
            vo.setSortOrder(tbContent.getSortOrder());

            //分类
            if (tbContent.getCategoryId() == null) {
                vo.setCategoryId2(null);
            }else {
                TbContentCategory cc = contentCategoryMapper.selectByPrimaryKey(tbContent.getCategoryId());
//                Long aLong = Long.valueOf(cc.getName());
                vo.setCategoryId2(cc.getName());
            }

            //状态
            if (tbContent.getStatus().equals("2")){
                vo.setStatus("无效");
            }
            if (tbContent.getStatus().equals("1")){
                vo.setStatus("有效");
            }

            list.add(vo);
        }
        return list;
//
    }

    @Override
    public void addContent(ContentVO vo) {
        TbContent content = new TbContent();
        BeanUtils.copyProperties(vo,content);
        contentMapper.insertSelective(content);
    }

    @Override
    public ContentVO toUpdate(Long id) {
        TbContent content = contentMapper.selectByPrimaryKey(id);
        ContentVO vo = new ContentVO();
        BeanUtils.copyProperties(content,vo);
        return vo;
    }

    @Override
    public void updateContent(ContentVO vo) {
        TbContent content = new TbContent();
        BeanUtils.copyProperties(vo,content);
        contentMapper.updateByPrimaryKeySelective(content);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        List<Long> list = Arrays.asList(ids);
        criteria.andIdIn(list);
        return contentMapper.deleteByExample(example);
    }

    @Override
    public List<ContentVO> getContentByCategoryId(Integer categoryId, Integer pageSize) {
        String contentList = (String) redis.boundValueOps("content" + categoryId).get();
        if(contentList==null || "[]".equals(contentList)){
            List<TbContent> contentByCategoryId = contentMapper.getContentByCategoryId(categoryId, pageSize);
            String s = JSONObject.toJSONString(contentByCategoryId);
            redis.opsForValue().set("content"+categoryId,s);
            List<ContentVO> vo = JSONObject.parseArray(JSON.toJSONString(contentByCategoryId), ContentVO.class);
            return vo;
        }
        return (List<ContentVO>) JSONObject.parse(contentList);
    }
}
